home *** CD-ROM | disk | FTP | other *** search
/ PD Collection CD 1 / PD Collection CD 1.iso / textual / pdftops / xpdf / h / Stream < prev    next >
Text File  |  1996-06-08  |  10KB  |  352 lines

  1. //========================================================================
  2. //
  3. // Stream.h
  4. //
  5. // Copyright 1996 Derek B. Noonburg
  6. //
  7. //========================================================================
  8.  
  9. #ifndef STREAM_H
  10. #define STREAM_H
  11.  
  12. #ifdef __GNUC__
  13. //#pragma interface
  14. #endif
  15.  
  16. #include <stdio.h>
  17. #include "gtypes.h"
  18.  
  19. #include "Object.h"
  20.  
  21. //------------------------------------------------------------------------
  22. // Stream (base class)
  23. //------------------------------------------------------------------------
  24.  
  25. class Stream {
  26. public:
  27.  
  28.   // Constructor.
  29.   Stream(): ref(1) {}
  30.  
  31.   // Destructor.
  32.   virtual ~Stream() {}
  33.  
  34.   // Reference counting.
  35.   int incRef() { return ++ref; }
  36.   int decRef() { return --ref; }
  37.  
  38.   // Reset stream to beginning.
  39.   virtual void reset() = 0;
  40.  
  41.   // Get next char from stream.
  42.   virtual int getChar() = 0;
  43.  
  44.   // Get current position in file.
  45.   virtual int getPos() = 0;
  46.  
  47.   // Go to a position in the stream.
  48.   virtual void setPos(int pos1);
  49.  
  50.   // Get PostScript command for the filter(s).
  51.   virtual GString *getPSFilter(char *indent);
  52.  
  53.   // Does this stream type potentially contain non-printable chars?
  54.   virtual GBool isBinary(GBool last = gTrue) = 0;
  55.  
  56.   // Get the base FileStream or SubStream of this stream.
  57.   virtual Stream *getBaseStream() = 0;
  58.  
  59.   // Get the base file of this stream.
  60.   virtual FILE *getFile() = 0;
  61.  
  62.   // Get the dictionary associated with this stream.
  63.   virtual Dict *getDict() = 0;
  64.  
  65.   // Add filters to this stream according to the parameters in <dict>.
  66.   // Returns the new stream.
  67.   Stream *addFilters(Object *dict);
  68.  
  69. private:
  70.  
  71.   Stream *makeFilter(char *name, Stream *str, Object *params);
  72.  
  73.   int ref;            // reference count
  74. };
  75.  
  76. //------------------------------------------------------------------------
  77. // FileStream
  78. //------------------------------------------------------------------------
  79.  
  80. class FileStream: public Stream {
  81. public:
  82.  
  83.   FileStream(FILE *f1, int start1, int length1, Object *dict1);
  84.   virtual ~FileStream();
  85.   virtual void reset();
  86.   virtual int getChar();
  87.   virtual int getPos() { return pos; }
  88.   virtual void setPos(int pos1);
  89.   virtual GBool isBinary(GBool last = gTrue) { return last; }
  90.   virtual Stream *getBaseStream() { return this; }
  91.   virtual FILE *getFile() { return f; }
  92.   virtual Dict *getDict() { return dict.getDict(); }
  93.  
  94.   // Check for a PDF header on this stream.  Skip past some garbage
  95.   // if necessary.
  96.   GBool checkHeader();
  97.  
  98.   // Get position of first byte of stream within the file.
  99.   int getStart() { return start; }
  100.  
  101. private:
  102.  
  103.   FILE *f;
  104.   int start;
  105.   int length;
  106.   int pos;
  107.   int savePos;
  108.   Object dict;
  109. };
  110.  
  111. //------------------------------------------------------------------------
  112. // SubStream
  113. //------------------------------------------------------------------------
  114.  
  115. class SubStream: public Stream {
  116. public:
  117.  
  118.   SubStream(Stream *str1, Object *dict1);
  119.   virtual ~SubStream();
  120.   virtual void reset() {}
  121.   virtual int getChar() { return str->getChar(); }
  122.   virtual int getPos() { return str->getPos(); }
  123.   virtual GBool isBinary(GBool last = gTrue) { return last; }
  124.   virtual Stream *getBaseStream() { return this; }
  125.   virtual FILE *getFile() { return str->getFile(); }
  126.   virtual Dict *getDict() { return dict.getDict(); }
  127.  
  128. private:
  129.  
  130.   Stream *str;
  131.   Object dict;
  132. };
  133.  
  134. //------------------------------------------------------------------------
  135. // ASCIIHexStream
  136. //------------------------------------------------------------------------
  137.  
  138. class ASCIIHexStream: public Stream {
  139. public:
  140.  
  141.   ASCIIHexStream(Stream *str1);
  142.   virtual ~ASCIIHexStream();
  143.   virtual void reset();
  144.   virtual int getChar();
  145.   virtual int getPos() { return str->getPos(); }
  146.   virtual GString *getPSFilter(char *indent);
  147.   virtual GBool isBinary(GBool last = gTrue);
  148.   virtual Stream *getBaseStream() { return str->getBaseStream(); }
  149.   virtual FILE *getFile() { return str->getFile(); }
  150.   virtual Dict *getDict() { return str->getDict(); }
  151.  
  152. private:
  153.  
  154.   Stream *str;
  155.   GBool eof;
  156. };
  157.  
  158. //------------------------------------------------------------------------
  159. // ASCII85Stream
  160. //------------------------------------------------------------------------
  161.  
  162. class ASCII85Stream: public Stream {
  163. public:
  164.  
  165.   ASCII85Stream(Stream *str1);
  166.   virtual ~ASCII85Stream();
  167.   virtual void reset();
  168.   virtual int getChar();
  169.   virtual int getPos() { return str->getPos(); }
  170.   virtual GString *getPSFilter(char *indent);
  171.   virtual GBool isBinary(GBool last = gTrue);
  172.   virtual Stream *getBaseStream() { return str->getBaseStream(); }
  173.   virtual FILE *getFile() { return str->getFile(); }
  174.   virtual Dict *getDict() { return str->getDict(); }
  175.  
  176. private:
  177.  
  178.   Stream *str;
  179.   Gulong c[5];
  180.   Gulong b[4];
  181.   int index, n;
  182.   GBool eof;
  183. };
  184.  
  185. //------------------------------------------------------------------------
  186. // LZWStream
  187. //------------------------------------------------------------------------
  188.  
  189. class LZWStream: public Stream {
  190. public:
  191.  
  192.   LZWStream(Stream *str1, int predictor1, int columns1, int colors1,
  193.         int bits1, int early1);
  194.   virtual ~LZWStream();
  195.   virtual void reset();
  196.   virtual int getChar();
  197.   virtual int getPos() { return str->getPos(); }
  198.   virtual GString *getPSFilter(char *indent);
  199.   virtual GBool isBinary(GBool last = gTrue);
  200.   virtual Stream *getBaseStream() { return str->getBaseStream(); }
  201.   virtual FILE *getFile() { return str->getFile(); }
  202.   virtual Dict *getDict() { return str->getDict(); }
  203.  
  204. private:
  205.  
  206.   Stream *str;            // stream
  207.   int predictor;        // parameters
  208.   int columns;
  209.   int colors;
  210.   int bits;
  211.   int early;
  212.   char zCmd[256];        // uncompress command
  213.   FILE *zPipe;            // uncompress pipe
  214.   char *zName;            // .Z file name (in zCmd)
  215.   int inputBuf;            // input buffer
  216.   int inputBits;        // number of bits in input buffer
  217.   int inCodeBits;        // size of input code
  218.  
  219.   void dumpFile(FILE *f);
  220.   int getCode();
  221. };
  222.  
  223. #if 0
  224. //------------------------------------------------------------------------
  225. // RunLengthStream
  226. //------------------------------------------------------------------------
  227.  
  228. class RunLengthStream: public Stream {
  229. };
  230. #endif
  231.  
  232. //------------------------------------------------------------------------
  233. // CCITTFaxStream
  234. //------------------------------------------------------------------------
  235.  
  236. struct CCITTCodeTable;
  237.  
  238. class CCITTFaxStream: public Stream {
  239. public:
  240.  
  241.   CCITTFaxStream(Stream *str1, int encoding1, GBool byteAlign1,
  242.          int columns1, int rows1, GBool black1);
  243.   virtual ~CCITTFaxStream();
  244.   virtual void reset();
  245.   virtual int getChar();
  246.   virtual int getPos() { return str->getPos(); }
  247.   virtual GString *getPSFilter(char *indent);
  248.   virtual GBool isBinary(GBool last = gTrue);
  249.   virtual Stream *getBaseStream() { return str->getBaseStream(); }
  250.   virtual FILE *getFile() { return str->getFile(); }
  251.   virtual Dict *getDict() { return str->getDict(); }
  252.  
  253. private:
  254.  
  255.   Stream *str;            // stream
  256.   int encoding;            // 'K' parameter
  257.   GBool byteAlign;        // 'EncodedByteAlign' parameter
  258.   int columns;            // 'Columns' parameter
  259.   int rows;            // 'Rows' parameter
  260.   GBool black;            // 'BlackIs1' parameter
  261.   GBool eof;            // true if at eof
  262.   int inputBuf;            // input buffer
  263.   int inputBits;        // number of bits in input buffer
  264.   short *refLine;        // reference line changing elements
  265.   int b1;            // index into refLine
  266.   short *codingLine;        // coding line changing elements
  267.   int a0;            // index into codingLine
  268.   int outputBits;        // remaining ouput bits
  269.  
  270.   short getCode(CCITTCodeTable *table);
  271.   int getBit();
  272. };
  273.  
  274. //------------------------------------------------------------------------
  275. // DCTStream
  276. //------------------------------------------------------------------------
  277.  
  278. // DCT component info
  279. struct DCTCompInfo {
  280.   int id;            // component ID
  281.   GBool inScan;            // is this component in the current scan?
  282.   int hSample, vSample;        // horiz/vert sampling resolutions
  283.   int quantTable;        // quantization table number
  284.   int dcHuffTable, acHuffTable;    // Huffman table numbers
  285.   int prevDC;            // DC coefficient accumulator
  286. };
  287.  
  288. // DCT Huffman decoding table
  289. struct DCTHuffTable {
  290.   Guchar firstSym[17];        // first symbol for this bit length
  291.   Gushort firstCode[17];    // first code for this bit length
  292.   Gushort numCodes[17];        // number of codes of this bit length
  293.   Guchar sym[256];        // symbols
  294. };
  295.  
  296. class DCTStream: public Stream {
  297. public:
  298.  
  299.   DCTStream(Stream *str1);
  300.   virtual ~DCTStream();
  301.   virtual void reset();
  302.   virtual int getChar();
  303.   virtual int getPos() { return str->getPos(); }
  304.   virtual GString *getPSFilter(char *indent);
  305.   virtual GBool isBinary(GBool last = gTrue);
  306.   virtual Stream *getBaseStream() { return str->getBaseStream(); }
  307.   virtual FILE *getFile() { return str->getFile(); }
  308.   virtual Dict *getDict() { return str->getDict(); }
  309.  
  310. private:
  311.  
  312.   Stream *str;            // stream
  313.   int width, height;        // image size
  314.   int mcuWidth, mcuHeight;    // size of min coding unit, in data units
  315.   DCTCompInfo compInfo[4];    // info for each component
  316.   int numComps;            // number of components in image
  317.   int colorXform;        // need YCbCr-to-RGB transform?
  318.   int restartInterval;        // restart interval, in MCUs
  319.   Guchar quantTables[4][64];    // quantization tables
  320.   int numQuantTables;        // number of quantization tables
  321.   DCTHuffTable dcHuffTables[4];    // DC Huffman tables
  322.   DCTHuffTable acHuffTables[4];    // AC Huffman tables
  323.   int numDCHuffTables;        // number of DC Huffman tables
  324.   int numACHuffTables;        // number of AC Huffman tables
  325.   Guchar *rowBuf[4][32];    // buffer for one MCU
  326.   int comp, x, y, dy;        // current position within image/MCU
  327.   int restartCtr;        // MCUs left until restart
  328.   int restartMarker;        // next restart marker
  329.   int inputBuf;            // input buffer for variable length codes
  330.   int inputBits;        // number of valid bits in input buffer
  331.  
  332.   void restart();
  333.   GBool readMCURow();
  334.   GBool readDataUnit(DCTHuffTable *dcHuffTable, DCTHuffTable *acHuffTable,
  335.              Guchar quantTable[64], int *prevDC, Guchar data[64]);
  336.   int readHuffSym(DCTHuffTable *table);
  337.   int readAmp(int size);
  338.   int readBit();
  339.   GBool readHeader();
  340.   GBool readFrameInfo();
  341.   GBool readScanInfo();
  342.   GBool readQuantTables();
  343.   GBool readHuffmanTables();
  344.   GBool readRestartInterval();
  345.   GBool readAdobeMarker();
  346.   GBool readTrailer();
  347.   int readMarker();
  348.   int read16();
  349. };
  350.  
  351. #endif
  352.